import { badminton } from './DataTypes'
import { Logger } from './Logger'
import { DecodeUserData, EncodeUserData } from './MessageUtils'
import { UserDataCheck } from './VersionController'

function buildRet ({ userData = '', ret = '', error = '', showMsg = '', boardUpdate = ''
  , prePayCheckRet = '', orderIds = '', logs = '', detailData = '', mails = ''}:
                       { userData?: string, ret?: string, error?: string, showMsg?: string, boardUpdate?: string
                        , prePayCheckRet?: string, orderIds?: string, logs?: string, detailData?: string, mails?: string }): string {
  const callRet = {
    userData,
    ret,
    error,
    showMsg,
    boardUpdate,
    prePayCheckRet,
    orderIds,
    logs,
    detailData,
    mails
  }
  return JSON.stringify(callRet)
}

function loadUserData (orgData: string): {dirty: boolean, userData: badminton.proto.IUserData} {
  let userData = DecodeUserData(orgData)
  return UserDataCheck(userData)
}

// 服务器定时拉取日志
export function loadLogs (): string {
  let logsArray : string[] = Logger.LoadData()
  let logs : string = JSON.stringify(logsArray)
  return buildRet({ logs })
}

export function initData (cfgData: any, gid: string, logLevel: number): string {
  Logger.setLevel(logLevel || 0)
  return buildRet({})
}

export function getRoleData (params: string, orgData: string): string {
  const {userData} = loadUserData(orgData)
  const str = EncodeUserData(userData)
  return buildRet({ret: str, userData : str})
}
